using AIFSContentManagementSystem;
using AIFSContentManagementSystem.Articles;
using AIFSContentManagementSystem.Criteria;
using AIFSDataAccessLayer;
using System;
using System.Collections.Generic;

namespace AIFSCMS.Infrastructure.Repositories
{
    public class RepositoryMap : IRepositoryMap
    {
        Dictionary<Type, IRepository> repositoryMap;

        public RepositoryMap(IDataExecutionContext DataExecutionContext)
        {
            //Create repository  map
            repositoryMap = new Dictionary<Type, IRepository>();

            ICriteriaService criteriaService = new CriteriaService(this);
            IArticleRepository articleRepository = new ArticleRepository(this, DataExecutionContext, criteriaService);
            repositoryMap.Add(typeof(IArticleRepository), articleRepository);

            ICriteriaRepository criteriaRespository = new CriteriaRepository(this, DataExecutionContext);
            repositoryMap.Add(typeof(ICriteriaRepository), criteriaRespository);
        }

        public T Get<T>() where T : IRepository
        {
            Type repositoryType = typeof(T);

            T repository = default(T);

            if (repositoryMap.ContainsKey(repositoryType))
            {
                repository = (T)repositoryMap[repositoryType];
            }

            if (repository == null)
            {
                throw new AIFSCMSInfrastructureException(string.Format("Repository of type {0} is not available", repositoryType.Name));
            }

            return repository;
        }

        bool disableLazyLoad;
        public bool DisableLazyLoad
        {
            get
            {
                return disableLazyLoad;
            }
            set
            {
                disableLazyLoad = value;
            }
        }
    }
}
